﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRule;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using Newtonsoft.Json.Linq;
using ProjectManApi.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManApi.Controllers.xmgl
{
    public class ProCaiGouController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public ProCaiGouController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }


        #region 导出

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> ExportList(dynamic expjson)
        {

            JObject job = (JObject)expjson;

            List<string> ids = new List<string>();
            List<ColumnModel> cols = new List<ColumnModel>();

            if (null != job["ids"])
                ids = job["ids"].ToObject<string[]>().ToList();

            if (null != job["cols"])
                cols = job["cols"].ToObject<ColumnModel[]>().ToList();

            return File(await CProTZJHRule.GetExportList(ids, cols, BaseEditPower.TableName.采购计划, "采购申请列表"), "application/ms-excel", "采购申请列表.xls");
        }

        #endregion

        #region 查询，新增，修改,删除，采购申请

        [HttpGet]
        //[Authorize(Policy = "TestPolicy")]
        //[AllowAnonymous]
        public async Task<IActionResult> GetXMCGIndexList(string formCode, string cgmc, string dxmc, string xmmc, string cgfs, string xmfl, string jsdw, string status, DateTime? begindate, DateTime? enddate, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                ProCaiGouQuerry querry = new ProCaiGouQuerry();
                //基础属性
                querry.size = CommonHelper.ParseInt(size);
                querry.current = CommonHelper.ParseInt(current);
                querry.sort = sort;
                querry.column = column;

                querry.cgmc = cgmc;
                querry.dxmc = dxmc;
                querry.cgfs = cgfs;

                querry.Jsdw = jsdw;
                querry.Xmmc = xmmc;
                querry.Xmfl = xmfl;
                querry.Status = status;
                querry.Begindate = begindate;
                querry.Enddate = enddate;

                (IEnumerable<dynamic>, int) SelectTable = await ProCaiGouRule.GetCaiGouIndexList(querry, await GetDepId());//GetUserId()



                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 根据采购计划id获取采购数据
        /// </summary>
        /// <param name="cgid"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMCGEntity(string cgid)//
        {
            try
            {
                var SelectTable = await ProCaiGouRule.GetXMCGEntity(cgid);
                //Page pa = new Page();
                //pa.records = SelectTable.Item1;
                //pa.total = SelectTable.Item2;
                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据单项id获取采购数据
        /// </summary>
        /// <param name="dxid"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMCGEntityBydxid(string dxid)//
        {
            //[FromForm] 
            try
            {
                var SelectTable = await ProCaiGouRule.GetXMCGEntityByDxid(dxid);
                //Page pa = new Page();
                //pa.records = SelectTable.Item1;
                //pa.total = SelectTable.Item2;
                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        ///// <summary>
        ///// 根据单项id获取采购数据
        ///// </summary>
        ///// <param name="dxid"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[AllowAnonymous]
        //public async Task<IActionResult> GetXMCGZBDLBydxid(string dxid)//
        //{
        //    //[FromForm] 
        //    try
        //    {
        //        var SelectTable = await ProCaiGouRule.GetXMCGZBDLBydxid(dxid);
        //        //Page pa = new Page();
        //        //pa.records = SelectTable.Item1;
        //        //pa.total = SelectTable.Item2;
        //        return Success(SelectTable);
        //    }
        //    catch (Exception)
        //    {
        //        return Fail("响应失败");
        //    }
        //}

        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> InsertCProCG2(CProZBCG cgmx)
        {
            return Fail("响应失败");
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProCG(dynamic cgxxjson)
        {
            string strope = "";
            CProZBLS cgxx = new CProZBLS();
            List<CProZBCG> cgmxs = new List<CProZBCG>();
            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)cgxxjson;

                //获取方法
                strope = cgxxjson.ope;

                //获取实体类 这样复杂类型不用定义实体类
                cgxx = job["cgxx"].ToObject<CProZBLS>();

                if (null != job["cgmxs"])
                    cgmxs = job["cgmxs"].ToObject<List<CProZBCG>>().ToList();

                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    //只能是[add,edit]
                    return Fail("ope参数类型错误");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(cgxx.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProZBLS jhe = await TranstionRule.FindEntityByKey<CProZBLS>(cgxx.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    cgxx.status = BaseEditPower.GetSPEnumValue("待审核");
                }


                string Cuid = GetUserId();
                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(cgxx.dxid))
                    {
                        return Fail("新增操作，dxid不能为空");
                    }


                    cgxx.id = CommonHelper.GetUniqueId().ToString();
                    cgxx.createuser = Cuid;
                    cgxx.createdate = DateTime.Now;

                    cgxx.cgbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.采购计划.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cgxxjson), await GetICApiInfo());

                    if (strlsope != "submit")
                    {
                        cgxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    for (int i = 0; i < cgmxs.Count; i++)
                    {
                        cgmxs[i].id = CommonHelper.GetUniqueId().ToString();
                        cgmxs[i].zbid = cgxx.id;
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cgxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.采购计划.ToString(), cgxx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxx.cgbm, amount = cgxx.cgje?.ToString(), content = "[项目采购]" + cgxx.cgmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.采购计划.ToString(), cgxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cgxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertCGXX(cgxx, cgmxs, strope);
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cgxx.id))
                    {
                        return Fail("修改操作，采购id不能为空");
                    }

                    if (strlsope != "submit")
                    {
                        cgxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(cgxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.采购计划.ToString(), cgxx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = cgxx.cgbm, amount = cgxx.cgje?.ToString(), content = "[项目采购]" + cgxx.cgmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.采购计划.ToString(), cgxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cgxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertCGXX(cgxx, cgmxs, strope);
                }



                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }


        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProCGMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.采购计划))
                    {
                        throw new Exception("请确认数据权限");
                    }

                }
                await BusinessRule.ProCaiGouRule.DeleteCProCGXXMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion

        #region 采购明细 新增 修改 删除
        /// <summary>
        /// 采购明细  新增修改删除
        /// </summary>
        /// <param name="cgxxmxjson"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> OPECProCGMX(dynamic cgxxmxjson)
        {
            string strope = "";
            CProZBCG cgmx = null;
            try
            {
                JObject job = (JObject)cgxxmxjson;

                //获取方法
                strope = cgxxmxjson.ope;

                //获取实体类 这样复杂类型不用定义实体类
                cgmx = job["cgmx"].ToObject<CProZBCG>();

                //cgmxs = job["cgmxs"].ToObject<List<CProZBCG>>().ToList();


            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                strope = strope.ToLower();
                if (strope != "add" && strope != "edit" && strope != "del")
                {
                    //只能是[add,edit]
                    return Fail("ope参数类型错误");
                }

                string Cuid = GetUserId();
                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(cgmx.zbid))
                    {
                        return Fail("修改操作，zbid不能为空");
                    }

                    cgmx.id = CommonHelper.GetUniqueId().ToString();
                    cgmx.createuser = Cuid;
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(cgmx.id))
                    {
                        return Fail("修改操作，明细id不能为空");
                    }
                    //if (string.IsNullOrEmpty(cgmx.zbid))
                    //{
                    //    return Fail("修改操作，zbid不能为空");
                    //}
                }
                else if (strope == "del")
                {
                    if (string.IsNullOrEmpty(cgmx.id))
                    {
                        return Fail("修改操作，明细id不能为空");
                    }
                }

                await BusinessRule.ProCaiGouRule.InsertCGXXMX(cgmx, strope);

                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }
        #endregion

        #region 项目编标

        [HttpGet]
        //[AllowAnonymous]
        //string dxmc, string xmmc, string cgmc, string xmfl, string jsdw, string status, DateTime? begindate, DateTime? enddate, string current = "1", string size = "20", string sort = "desc", string column = "id"
        public async Task<IActionResult> GetXZDXNoBBIndexList([FromQuery] ProBianbiaoQuerry querry)
        {
            try
            {
               

                (IEnumerable<dynamic>, int) SelectTable = await ProCaiGouRule.GetXZDXNoBBIndexList(querry, await GetDepId());//GetUserId()

             
                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        //[Authorize(Policy = "TestPolicy")]
        //[AllowAnonymous]
        //string formCode, string cgmc, string dxmc, string xmmc, string cgfs, string cglb, string xmfl, string jsdw, string bbzt, string status, DateTime? begindate, DateTime? enddate, string current = "1", string size = "20", string sort = "desc", string column = "id"
        public async Task<IActionResult> GetXMBBIndexList([FromQuery] ProBianbiaoQuerry querry)
        {
            try
            {
             

                (IEnumerable<dynamic>, int) SelectTable = await ProCaiGouRule.GetBianbiaoIndexList(querry, await GetDepId());//GetUserId()



                List<string> list = new List<string>();
                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 根据采购计划id获取编标数据
        /// </summary>
        /// <param name="bbid"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMBBEntity(string bbid)//
        {
            try
            {
                var SelectTable = await ProCaiGouRule.GetXMBBEntity(bbid);

                try
                {
                    SelectTable.xmxx = await ProCaiGouRule.GetBDByBBId(bbid);
                }
                catch
                {
                }
                try
                {
                    SelectTable.cgxx = await ProCaiGouRule.GetCGXXByBBId(bbid);
                }
                catch
                {
                }


                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据单项id获取编标数据
        /// </summary>
        /// <param name="dxid"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMBBEntityBydxid(string dxid)//
        {
            //[FromForm] 
            try
            {
                var SelectTable = await ProCaiGouRule.GetXMBBEntityByDxid(dxid, "cq");

                if (null == SelectTable)
                {
                    SelectTable = new ExpandoObject();
                }
                try
                {
                    // 标段信息
                    SelectTable.xmxx = await ProCaiGouRule.GetBDById(dxid);

                }
                catch
                {
                }
                try
                {
                    SelectTable.cgxx = await ProCaiGouRule.GetCGXXById(dxid);
                }
                catch
                {
                }


                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 根据单项id获取编标最终信息
        /// </summary>
        /// <param name="dxid"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> GetXMBBZZXXBydxid(string dxid)//
        {
            //[FromForm] 
            try
            {
                //查询最终数据
                var SelectTable = await ProCaiGouRule.GetXMBBEzzByDxid(dxid);//"zz"

                try
                {
                    SelectTable.xmxx = await ProCaiGouRule.GetBDById(dxid);
                }
                catch
                {
                }
                try
                {
                    SelectTable.cgxx = await ProCaiGouRule.GetCGXXById(dxid);
                }
                catch
                {
                }


                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProBB(dynamic bbxxjson)
        {
            string strope = "";
            CProZBBianB bbxx = new CProZBBianB();
            List<CProZBBianBMX> bbmxs = new List<CProZBBianBMX>();
            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)bbxxjson;

                //获取方法
                strope = bbxxjson.ope;

                //获取实体类 这样复杂类型不用定义实体类
                bbxx = job["bbxx"].ToObject<CProZBBianB>();

                if (null != job["bbmxs"])
                    bbmxs = job["bbmxs"].ToObject<List<CProZBBianBMX>>().ToList();

                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit" && strope != "submit")
                {
                    //只能是[add,edit]
                    return Fail("ope参数类型错误");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(bbxx.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProZBBianB jhe = await TranstionRule.FindEntityByKey<CProZBBianB>(bbxx.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    bbxx.status = BaseEditPower.GetSPEnumValue("待审核");
                }


                string Cuid = GetUserId();
                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(bbxx.dxid))
                    {
                        return Fail("新增操作，dxid不能为空");
                    }


                    bbxx.id = CommonHelper.GetUniqueId().ToString();
                    //CProZBBianB
                    bbxx.bm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.编标计划.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(bbxxjson), await GetICApiInfo());

                    bbxx.createuser = GetUserName();
                    bbxx.createdate = DateTime.Now;

                    bbxx.lx = "cq";

                    if (strlsope != "submit")
                    {
                        bbxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    for (int i = 0; i < bbmxs.Count; i++)
                    {
                        bbmxs[i].id = CommonHelper.GetUniqueId().ToString();
                        bbmxs[i].bbid = bbxx.id;
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(bbxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, bbxx.dxid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bbxx.bm, amount = bbxx.je?.ToString(), content = "[项目编标]" + bbxx.mc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(bbxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertBBXX(bbxx, bbmxs, strope, "cq");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(bbxx.id))
                    {
                        return Fail("修改操作，编标id不能为空");
                    }

                    if (strlsope != "submit")
                    {
                        bbxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(bbxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        //给一个明细id
                        CProZBBianB jhe = await TranstionRule.FindEntityByKey<CProZBBianB>(bbxx.id);
                        string strdxid = "";
                        if (null != jhe)
                        {
                            strdxid = jhe.dxid;
                        }

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, strdxid);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bbxx.bm, amount = bbxx.je?.ToString(), content = "[项目采购]" + bbxx.mc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(bbxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertBBXX(bbxx, bbmxs, strope, "cq");
                }



                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }

        [HttpPost]
        public IActionResult InsertCProBBZZ1(CProZBBianB cc)
        {
            return Success("1");
        }

        [HttpPost]
        public IActionResult InsertCProBBZZ2(CProZBBianBMX cc)
        {
            return Success("1");
        }

        [HttpPost]
        //[AllowAnonymous]
        public async Task<IActionResult> InsertCProBBZZ(dynamic bbxxjson)
        {
            string strope = "";
            CProZBBianB bbxx = new CProZBBianB();
            List<CProZBBianBMX> bbmxs = new List<CProZBBianBMX>();
            //添加附件上传
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)bbxxjson;

                //获取方法
                strope = bbxxjson.ope;

                //获取实体类 这样复杂类型不用定义实体类
                bbxx = job["bbxx"].ToObject<CProZBBianB>();

                if (null != job["bbmxs"])
                    bbmxs = job["bbmxs"].ToObject<List<CProZBBianBMX>>().ToList();

                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (strope != "add" && strope != "edit")//&& strope != "submit"
                {
                    //只能是[add,edit]
                    return Fail("ope参数类型错误");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(bbxx.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProZBBianB jhe = await TranstionRule.FindEntityByKey<CProZBBianB>(bbxx.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    bbxx.status = BaseEditPower.GetSPEnumValue("待审核");
                }


                string Cuid = GetUserId();
                if (strope == "add")
                {
                    if (string.IsNullOrEmpty(bbxx.dxid))
                    {
                        return Fail("新增操作，dxid不能为空");
                    }


                    bbxx.id = CommonHelper.GetUniqueId().ToString();
                    bbxx.createuser = Cuid;

                    bbxx.lx = "zz";

                    if (strlsope != "submit")
                    {
                        bbxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    for (int i = 0; i < bbmxs.Count; i++)
                    {
                        bbmxs[i].id = CommonHelper.GetUniqueId().ToString();
                        bbmxs[i].bbid = bbxx.id;
                    }

                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(bbxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.编标计划.ToString(), bbxx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bbxx.bm, amount = "", content = "[项目编标]" + bbxx.mc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(bbxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertBBXX(bbxx, bbmxs, strope, "zz");
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(bbxx.id))
                    {
                        return Fail("修改操作，编标id不能为空");
                    }

                    if (strlsope != "submit")
                    {
                        bbxx.status = BaseEditPower.GetSPEnumValue("待提交");
                    }


                    //这里先绑定附件
                    await JavaApi.UploadFileJavaAPI(bbxx.id, filecodes, await GetICApiInfo());

                    bool bsp = true;
                    if (strlsope == "submit")
                    {
                        bsp = false;

                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.编标计划.ToString(), bbxx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = bbxx.bm, amount = "", content = "[项目采购]" + bbxx.mc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.编标计划.ToString(), bbxx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(bbxxjson), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);
                        bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        if (!bsp)
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    await BusinessRule.ProCaiGouRule.InsertBBXX(bbxx, bbmxs, strope, "zz");
                }



                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }


        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProBBMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.编标计划))
                    {
                        throw new Exception("请确认数据权限");
                    }

                }
                await BusinessRule.ProCaiGouRule.DeleteCProBBXXMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion


    }
}
